#include "Enemy.h"
#include "Game.h"
void Enemy::update(float frameTime)
{
	CXBOXController* controller;
	controller = new CXBOXController(1);
	const InputHandler* inputHandler = game->getInputHandler();
	Game* instance = (Game*)game;
	// Get
	std::list<Node*> connectedNodes = m_baseNode->getConnectedNodes();
	connectedNodes.push_back(m_prevNode); // add previous node as well

	bool moveToNext = false;

	if (this->m_baseNode->getNodeState() == Node::ENEMY ||
		m_baseNode->getSecuringState() == Node::SECURED || 
		m_currentNode->getSecuringState() == Node::SECURED || 
		m_currentNode->getNodeState() == Node::ENEMY)
	{
		moveToNext = true;
	}

	float leftX = controller->GetState().Gamepad.sThumbLX;
	float leftY = controller->GetState().Gamepad.sThumbLY;
	//D3DXVECTOR3
	
	XINPUT_KEYSTROKE keystroke;
	XInputGetKeystroke(0, XINPUT_FLAG_GAMEPAD, &keystroke);

	if (moveToNext)
	{
		if (keystroke.Flags == XINPUT_KEYSTROKE_KEYDOWN &&  controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_RIGHT)
		{
			wasStickRight = true;
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end())
			{
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();
				if (getPosition().y == nodePos.y && getPosition().x < nodePos.x && m_baseNode != *iter)
				{
					m_prevNode = m_baseNode;
					m_baseNode = *iter;
					m_currentNode = *iter;
				}
				++iter;
			}
		}
		else if (keystroke.Flags == XINPUT_KEYSTROKE_KEYDOWN && controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_LEFT)
		{
			wasStickLeft = true;
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end())
			{
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();
				if (getPosition().y == nodePos.y && getPosition().x > nodePos.x && m_baseNode != *iter)
				{
					m_prevNode = m_baseNode;
					m_baseNode = *iter;
					m_currentNode = *iter;
				}
				++iter;
			}
		}
		else if (keystroke.Flags == XINPUT_KEYSTROKE_KEYDOWN && controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_DOWN)
		{
			wasStickDown = true;
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end())
			{
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();
				if (getPosition().y < nodePos.y && getPosition().x == nodePos.x && m_baseNode != *iter)
				{
					m_prevNode = m_baseNode;
					m_baseNode = *iter;
					m_currentNode = *iter;
				}
				++iter;
			}
		}
		else if ( keystroke.Flags == XINPUT_KEYSTROKE_KEYDOWN && controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_UP)
		{
			wasStickUp = true;
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end())
			{
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();
				if (getPosition().y > nodePos.y && getPosition().x == nodePos.x && m_baseNode != *iter)
				{
					m_prevNode = m_baseNode;
					m_baseNode = *iter;
					m_currentNode = *iter;
				}
				++iter;
			}
		}
		moveToNext = false;
	}
	else
	{
		// can not move forward only backwards
		if (keystroke.Flags == XINPUT_KEYSTROKE_KEYDOWN && 
			controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_RIGHT)
		{
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end())
			{
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();
				if (getPosition().y == nodePos.y && getPosition().x < nodePos.x && m_baseNode != *iter && m_prevNode == *iter)
				{
					m_prevNode = m_baseNode;
					m_baseNode = *iter;
					m_currentNode = *iter;
				}
				iter++;
			}
		}
		else if (keystroke.Flags == XINPUT_KEYSTROKE_KEYDOWN && 
			controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_LEFT)
		{
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end())
			{
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();
				if (getPosition().y == nodePos.y && getPosition().x > nodePos.x && m_baseNode != *iter && m_prevNode == *iter)
				{
					m_prevNode = m_baseNode;
					m_baseNode = *iter;
					m_currentNode = *iter;
				}
				iter++;
			}
		}
		else if (keystroke.Flags == XINPUT_KEYSTROKE_KEYDOWN && controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_DOWN)
		{
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end())
			{
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();
				if (getPosition().y < nodePos.y && getPosition().x == nodePos.x && m_baseNode != *iter && m_prevNode == *iter)
				{
					m_prevNode = m_baseNode;
					m_baseNode = *iter;
					m_currentNode = *iter;
				}
				++iter;
			}
		}
		else if (keystroke.Flags == XINPUT_KEYSTROKE_KEYDOWN && controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_DPAD_UP)
		{
			std::list<Node*>::iterator iter = connectedNodes.begin();
			while (iter != connectedNodes.end())
			{
				Node* temp = (*iter);
				D3DXVECTOR3 nodePos = temp->getPosition();
				if (getPosition().y > nodePos.y && getPosition().x == nodePos.x && m_baseNode != *iter && m_prevNode == *iter)
				{
					m_prevNode = m_baseNode;
					this->setBaseNode(*iter);
					this->m_currentNode = *iter;
				}
				++iter;
			}
		}
	}
	anims->playAnimation("Focus");
	setPosition(m_currentNode->getPosition());

	if (keystroke.Flags == XINPUT_KEYSTROKE_KEYDOWN && controller->GetState().Gamepad.wButtons == XINPUT_GAMEPAD_A)
	{
		if (m_baseNode->getSecuringState() != Node::SECURED && m_baseNode->getSecuringState() != Node::SECURING)
		{
			Message message(this->m_currentNode, "Securing", 0);
			MessageHandler::sendMessage(message);
		}
	}

	if (m_currentNode == m_baseNode)
		anims->playAnimation("Focus");
}

void Enemy::draw(ID3DXSprite* sprite)
{
	//First we work out the centre of the frame
	D3DXVECTOR2 centre;
	centre.x = (float)(anims->getCurrentAnimation()->getFrameWidth() / 2);
	centre.y = (float)(anims->getCurrentAnimation()->getFrameHeight() / 2);

	/* Now we create a transformation matrix. If flipped, we scale X by -1
	 * we also use the position vector in our transaction */
	D3DXMATRIX mat;
	/*if(flip)
		D3DXMatrixTransformation2D(&mat, &centre, 0,
								   &D3DXVECTOR2(-1, 1), 0, 0,
								   &D3DXVECTOR2(position.x, position.y));
	else */
		D3DXMatrixTransformation2D(&mat, 0, 0, 0, 0, 0,
								   &D3DXVECTOR2(position.x, position.y));

	//Now we use this transformation to draw with, we set the draw position to 0 to avoid issues
	
	anims->draw(sprite, D3DXVECTOR3(0, 0, 0), mat);
}

void Enemy::handleMessage(Message message)
{
	// HANDLE MESSAGES...
	if (message.message == "NodeHacked")
	{
		Node* node = (Node*)message.data;
		
		for (int i = 0; i < m_securedNodes.size(); ++i)
		{
			if (m_securedNodes.front() == node)
			{
				m_securedNodes.pop_front();
				i = m_securedNodes.size();
			}
			else
				m_securedNodes.pop_front();
		}
		// Implement knocking nodes off hacked list.
	}

	if (message.message == "NodeSecured")
	{
		Node* node = (Node*)message.data;
		if (node->getOwner() == Node::FINISH)
		{
			anims->playAnimation("Idle");
			m_securedNodes.push_front(node);
		//	m_baseNode = node;
		}
		else
		{
			if (node != m_securedNodes.front())
			{
				m_securedNodes.push_front(node);
			//	m_baseNode = node;
			}
		}
	}
}